/**
 * @filename:SuppliterController 2021-11-04 01:53:18
 * @project com/lwl/blue  V1.0
 * Copyright(c) 2020 <a href=mailto:lanwenliang@yidianlife.com>蓝文良</a> Co. Ltd.
 * All right reserved.
 */
package com.lwl.blue.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwl.blue.commont.base.*;
import com.lwl.blue.entity.Supplier;
import com.lwl.blue.service.SupplierService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>自定义方法写在这里</p>
 *
 * <p>说明： API接口层</P>
 *
 * @version: V1.0
 * @author: <a href=mailto:lanwenliang@yidianlife.com>蓝文良</a>
 */
@RestController
@RequestMapping("/supplier")
@Slf4j
public class SupplierController extends BaseController {
    @Autowired
    SupplierService supplierService;

    @PostMapping("/pages")
    public PageResult<Supplier> getPages(@RequestBody PageParam<Supplier> pageParam) {
        JsonResult<Page<Supplier>> pages = supplierService.pages(pageParam);
        PageResult<Supplier> re = new PageResult<>();
        re.success(pages.getData().getTotal(), pages.getData().getRecords());
        return re;
    }

    @PostMapping("saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody Supplier supplier) {
        setInsetOrUpdateUser(supplier);
        QueryWrapper<Supplier> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Supplier::getSupplierNo, supplier.getSupplierNo())
                .eq(Supplier::getIsDelete, 0);
        if (null != supplier.getId()) {
            wrapper.lambda().notIn(Supplier::getId, supplier.getId());
        }
        int count = supplierService.count(wrapper);
        if (count > 0) {
            throw new BizException(ErrorCode.DATA_EXISTS_ERROR.getCode(), ErrorCode.DATA_EXISTS_ERROR.getMsg() + supplier.getSupplierNo());
        }
        supplierService.saveOrUpdate(supplier);
        return new JsonResult().success("请求成功");
    }

    @PostMapping("/delete/{id}")
    public JsonResult saveOrUpdate(@PathVariable(value = "id") Integer id) {
        supplierService.update(new UpdateWrapper<Supplier>().lambda().set(Supplier::getIsDelete, Const.IS_DELETE_YES)
                .eq(Supplier::getId, id));
        return new JsonResult().success("请求成功");
    }

    @PostMapping("/select")
    public JsonResult select() {
        List<Supplier> reList = supplierService.list(new QueryWrapper<Supplier>()
                .lambda().eq(Supplier::getIsDelete, 0));
        return new JsonResult().success(reList);
    }

    /**
     * 供应商_导入
     *
     * @param file
     * @return
     */
    @GetMapping("export")
    public JsonResult export(@RequestParam("file") MultipartFile file) {
        return supplierService.export(file);

    }
}
